<template>
  <div ref="wrapper_hook" class="echarts_wrapper"/>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'vehicleBar',
  props: {
    seriesName: {
      type: String,
      default: '订单'
    },
    source: {
      type: Object,
      default: () => {
        return {
          seats: [],
          maxPrice: [],
          avgResult: [],
          plate:[]
        }
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    source(newValue) {
      this._initECharts(newValue)
    }
  },
  mounted() {
    this._initECharts(this.source)
    window.addEventListener('resize', this.resize)
    this.$once('hook:beforeDestroy', () => {
      window.removeEventListener('resize', this.resize)
    })
  },
  destroyed() {
    this.chart.dispose()
  },
  methods: {
    _initECharts(data) {
      this.chart ? this.update(data) : this.draw(data)
    },
    resize(delay = 400) {
      setTimeout(() => {
        this.chart && this.chart.resize()
      }, delay)
    },
    draw(data) {
      const {seats, maxPrice, avgResult, plate} = data
      this.chart = echarts.init(this.$refs.wrapper_hook)
      const options = {
        toolbox: {
          show: true
        },
        tooltip: {
          trigger: 'item',
          axisPointer: {
            type: 'shadow'
          },
          formatter: params =>  {
            return `${params.name}座(${plate[params.dataIndex]}) ${params.data}/${maxPrice[params.dataIndex]}`
          }
        },
        grid: {
          top: 40,
          left: 40,
          right: 0,
          bottom: 20
        },
        yAxis: {
          type: 'value',
          name:'单位:万',
          nameGap: 14,
          nameTextStyle:{
            fontSize: 14
          },
          axisLabel: {
            fontSize: 12
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#575656'
            }
          },
          axisTick: {
            show: false
          }
        },
        xAxis: {
          type: 'category',
          data: seats,
          axisLabel: {
            fontsize: 14,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#575656'
            }
          }
        },
        series: [
          {
            type: 'bar',
            data: avgResult,
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(180, 180, 180, 0.1)'
            },
            barWidth:40,
            label: {
              normal: {
                show: true,
                position: "top",
                textStyle: {
                  fontSize: 13,
                  color: "#787878",
                }
              }
            },
            itemStyle: {
              color: '#28DC32FF'
            }
          },
          {
            type: 'bar',
            data: maxPrice,
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(180, 180, 180, 0.1)'
            },
            barWidth:40,
            label: {
              normal: {
                show: true,
                formatter: params => {
                  return `${plate[params.dataIndex]}\n ${maxPrice[params.dataIndex]}`
                },
                position: "top",
                textStyle: {
                  fontSize: 13,
                  color: "#787878",
                }
              }
            },
            itemStyle: {
              color: '#f9c14e'
            }
          }
        ]
      }
      this.chart.setOption(options)
      this.chart.on('click', params => {
        this.$emit('change', params)
      })
    },
    async update(data) {
      if (!this.chart) return
      const {seats, maxPrice, avgResult,plate} = data
      const options = this.chart.getOption()
      let  name = '单位:万'

      if(this.seriesName === '里程'){
          name = '单位:公里'
      }

      options.yAxis[0].name = name
      options.tooltip[0].formatter = params => {
        return `${params.name}座(${plate[params.dataIndex]}) ${params.data}/${maxPrice[params.dataIndex]}`
      }
      options.xAxis[0].data = seats
      options.series[0].data = avgResult
      options.series[0].itemStyle.color = '#28DC32'

      options.series[1].data = maxPrice
      options.series[1].label.formatter = params => {
        return `${plate[params.dataIndex]}\n ${maxPrice[params.dataIndex]}`
      }
      options.series[1].itemStyle.color = '#F9C14E'
      await this.$nextTick()
      this.chart.setOption(options, true)
    }
  }
}
</script>

<style scoped>
.echarts_wrapper {
  width: 100%;
  height: 270px;
}
</style>

